<template>
  <div class="news_container">
    <div style="margin-bottom: 10px"><span style="font-size: 30px" @click="back">{{news_name}}</span></div>
    <div v-for="(item,key) in news" :key="key">
      <el-card shadow="hover" style="margin-bottom: 10px">
        <h3 style="margin-top: -15px">
          <router-link tag="span" :to="'/newsdetail/id/'+item.id+'/newsType/'+sqlname" class="news-title">{{item.title}}</router-link>
        </h3>
        <el-row class="news-body">
          <div class="myimg">
            <img class="news-img" v-show="item.img===null ? false:true" :src="item.img">
          </div>
          <div>
            <div>
                {{item.description}}
            </div>
          </div>
        </el-row>
        <el-row style="display: flex;justify-content: flex-end;margin-bottom: -10px">
          <div style="margin-right: 10px"><i class="el-icon-user-solid"></i>: {{item.author}}</div>
          <div><i class="el-icon-time"></i>：{{item.pubtime}}</div>
        </el-row>
      </el-card>
    </div>
    <div class="page">
        <el-pagination
          @current-change="handleCurrentChange($event,index)"
          :background="true"
          layout="prev, pager, next"
          :current-page="currentPage"
          :page-size="pageSize"
          :total="total">
        </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      news: [],
      news_name: this.$route.params.news_type,
      sqlname: this.$route.params.sql,
      kind: this.$route.params.kind,
      total: 0,
      currentPage: 1,
      pageSize: 30
    }
  },
  methods: {
    getNews (newsType, mykind) {
      this.$axios.get('news/getNews', {
        params: {
          current: 1,
          size: 30,
          news_type: newsType,
          kind: mykind
        }
      }).then(response => {
        console.log(response.data)
        this.$set(this, 'news', response.data.data.records)
        this.total = response.data.data.total
        this.pageSize = response.data.data.size
        this.currentPage = response.data.data.current
      }).catch(error => {
        console.log(error)
      })
    },
    handleCurrentChange (val, index) {
      this.$axios.get('news/getNews', {
        params: {
          current: val,
          size: 30,
          news_type: this.sqlname,
          kind: this.kind
        }
      }).then(response => {
        console.log(response.data)
        this.$set(this, 'news', response.data.data.records)
        this.total = response.data.data.total
        this.pageSize = response.data.data.size
        this.currentPage = response.data.data.current
      }).catch(error => {
        console.log(error)
      })
    },
    back () {
      this.$router.go(-1)
    }
  },
  mounted () {
    this.getNews(this.$route.params.sql, this.$route.params.kind)
  }
}
</script>

<style lang="less" scoped>
.news_container{
  margin-top: 20px;
  margin-left: 60px;
  margin-right: 60px;
}
.news-body {
display: flex;
padding: 0px 10px;
}
.news-img {
width: 150px;
height: 100px;
margin-right: 20px;
}
.page{
  text-align: center;
}
.news-title{
padding-left: 5px;
cursor: pointer;
}
</style>
